package com.nanbei.eduService.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nanbei.eduService.entity.EduCourse;
import com.nanbei.eduService.entity.EduCourseDescription;
import com.nanbei.eduService.entity.frontVo.CourseFrontQueryVo;
import com.nanbei.eduService.entity.frontVo.CourseWebVo;
import com.nanbei.eduService.entity.vo.CourseInfoVo;
import com.nanbei.eduService.entity.vo.CoursePublishVo;
import com.nanbei.eduService.mapper.EduCourseMapper;
import com.nanbei.eduService.service.EduChapterService;
import com.nanbei.eduService.service.EduCourseDescriptionService;
import com.nanbei.eduService.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nanbei.eduService.service.EduVideoService;
import com.nanbei.servicebase.exceptionHandler.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-12-29
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    private EduCourseDescriptionService eduCourseDescriptionService;

    @Autowired
    private EduVideoService eduVideoService;

    @Autowired
    private EduChapterService eduChapterService;

    /**
     * 添加课程基本信息
     * @param courseInfoVo
     */
    @Override
    public String addCourse(CourseInfoVo courseInfoVo) {

        //往课程表里面添加信息
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int insert = baseMapper.insert(eduCourse);
        if (insert == 0) {
            throw new GuliException(20001,"添加失败");
        }


        //往课程简介表里面添加信息
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        BeanUtils.copyProperties(courseInfoVo,eduCourseDescription);

        //课程表和课程简介表是一对一的关系，要求课程简介ID和课程表ID相同
        eduCourseDescription.setId(eduCourse.getId());
        eduCourseDescriptionService.save(eduCourseDescription);
        return eduCourse.getId();
    }

    /**
     * 根据课程Id查询课程基本信息
     * @param courseId
     * @return
     */
    @Override
    public CourseInfoVo findCourseById(String courseId) {
        //查询课程基本信息
        EduCourse eduCourse = baseMapper.selectById(courseId);
        //查询课程详情信息
        EduCourseDescription eduCourseDescription = eduCourseDescriptionService.getById(courseId);

        // 将查询到的信息添到Vo表中
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        BeanUtils.copyProperties(eduCourse,courseInfoVo);
        BeanUtils.copyProperties(eduCourseDescription,courseInfoVo);

        return courseInfoVo;
    }

    /**
     * 跟新课程进本信息
     * @param courseInfoVo
     */
    @Override
    public void updateCourseById(CourseInfoVo courseInfoVo) {
        //将Vo类的信息复制到实体类中进行根新
        EduCourse eduCourse = new EduCourse();
        EduCourseDescription eduCourseDescription = new EduCourseDescription();

        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        BeanUtils.copyProperties(courseInfoVo,eduCourseDescription);

        baseMapper.updateById(eduCourse);
        eduCourseDescriptionService.updateById(eduCourseDescription);

    }

    //根据ID查询发布课程的信息
    @Override
    public CoursePublishVo selectCoursePublishVoById(String courseId) {
        CoursePublishVo coursePublishVo = baseMapper.selectCoursePublishVoById(courseId);
        return coursePublishVo;
    }

    /**
     * 根据课程ID删除课程信息
     * @param courseId
     */
    @Override
    public void delCourseById(String courseId) {
        //先删除小节信息
        eduVideoService.delVideoByCourseById(courseId);

        //删除章节信息
        eduChapterService.delChapterByCourseById(courseId);

        //删除详情信息
        eduCourseDescriptionService.removeById(courseId);

        //删除课程
        this.baseMapper.deleteById(courseId);
    }

    @Override
    public IPage<EduCourse> pageCourseFront(Page<EduCourse> eduCoursePage, CourseFrontQueryVo courseFrontQueryVo) {

        QueryWrapper<EduCourse> eduCourseQueryWrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(courseFrontQueryVo.getSubjectParentId())) {
            eduCourseQueryWrapper.eq("subject_parent_id", courseFrontQueryVo.getSubjectParentId());
        }

        if (!StringUtils.isEmpty(courseFrontQueryVo.getSubjectId())) {
            eduCourseQueryWrapper.eq("subject_id", courseFrontQueryVo.getSubjectId());
        }

        if (!StringUtils.isEmpty(courseFrontQueryVo.getBuyCountSort())) {
            eduCourseQueryWrapper.orderByDesc("buy_count");
        }

        if (!StringUtils.isEmpty(courseFrontQueryVo.getGmtCreateSort())) {
            eduCourseQueryWrapper.orderByDesc("gmt_create");
        }

        if (!StringUtils.isEmpty(courseFrontQueryVo.getPriceSort())) {
            eduCourseQueryWrapper.orderByDesc("price");
        }

        IPage<EduCourse> eduCourseIPage = this.baseMapper.selectPage(eduCoursePage, eduCourseQueryWrapper);

        return eduCourseIPage;
    }

    @Override
    public CourseWebVo findCourseInfo(String courseId) {
        CourseWebVo courseWebVo = baseMapper.findCourseInfo(courseId);
        return courseWebVo;
    }
}
